package com.wxxymaker.controller;

import com.wxxymaker.utils.web.ResponseWarp;
import com.wxxymaker.utils.web.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Soft
 * @Date: 2018/5/3 12:36
 * @Desc: 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    // 运行时异常，服务器内部异常
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public String _500(RuntimeException e) {
        log.error("500: {}", e);
        return "/error/500";
    }

    // 运行时异常，服务器内部异常
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResponseWarp serviceExp(ServiceException e) {
        log.error("ServiceExp: {}", e.getMessage());
        return ResponseWarp.error(e.getMessage());
    }

    @RequestMapping("/noAccess")
    public ResponseWarp noAccess() {
        return ResponseWarp.error("没有权限！");
    }
}
